package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.DealScene;
import cn.net.shuxing.yifu.common.constant.OssConstant;
import cn.net.shuxing.yifu.common.constant.PriceRaito;
import cn.net.shuxing.yifu.common.constant.RaitoList;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.dto.CodeConst;
import cn.net.shuxing.yifu.common.service.*;
import cn.net.shuxing.yifu.common.util.FileUtil;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import cn.net.shuxing.yifu.common.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import cn.net.shuxing.yifu.common.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping("/back/dealparam")
public class DealParamController {

    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private CoinRatioService coinRatioService;

    @Autowired
    private CoinTypeService coinTypeService;

    @Autowired
    private CoinCommonService coinCommonService;

    @Autowired
    private OpenPriceHistoryService openPriceHistoryService;

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private OssCommonService ossCommonService;

    @Autowired
    private FeeService feeService;


    @RequestMapping("/cointype/changeTotalDealCount")
    public BaseDto changeTotalDealCount(Integer coinTypeId, String count) {
        try {
            BigDecimal count1 = new BigDecimal(count);
            jedisUtil.set(RedisKeyCreator.totalDealCount(coinTypeId), count);
            jedisUtil.set(RedisKeyCreator.totalDealCountUpdateTime(coinTypeId), LocalDateTime.now().toString());
            return BaseDto.ok("success");
        } catch (Exception e) {
            return BaseDto.error("count不是一个数字");
        }
    }

    @RequestMapping("/cointype/changeTodayDealCount")
    public BaseDto changeTodayDealCount(Integer coinTypeId, String count) {
        try {
            BigDecimal count1 = new BigDecimal(count);
            jedisUtil.set(RedisKeyCreator.todayDealCount(coinTypeId), count);
            jedisUtil.set(RedisKeyCreator.todayDealCountUpdateTime(coinTypeId), LocalDateTime.now().toString());
            return BaseDto.ok("success");
        } catch (Exception e) {
            return BaseDto.error("count不是一个数字");
        }
    }

    @RequestMapping("/cointype/changeCoinPrice")
    public BaseDto changeCoinPrice(Integer coinTypeId, String price) {
        try {
            BigDecimal price1 = new BigDecimal(price);
            openPriceHistoryService.insert(coinTypeId, price1);
            return BaseDto.ok("success");
        } catch (Exception e) {
            return BaseDto.error("count不是一个数字");
        }
    }

    //列表展示：编号、货币图标、货币名称、
    // 货币单价、已配发、已激活、已释放、
    // 已冻结、已回购、已交易、今日交易、
    // 创建时间、操作
    @RequestMapping("/cointype/list")
    public BaseDto coinTypeList(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                @RequestParam(defaultValue = "15",value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        List<CoinType> all = coinTypeService.getAllWithoutComplete();
        ArrayList<CoinCompletedInfo> list = new ArrayList<>();
        for (CoinType coinType : all) {
            list.add(coinCommonService.createCoinCompletedInfo(coinType.getId()));
        }
        //Page page = PageHelper.startPage(pageNum,pageSize);
        PageInfo<CoinCompletedInfo> pageInfo = new PageInfo<>(list);
        //pageInfo.setTotal(page.getTotal());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    @RequestMapping("/cointype/editlist")
    public BaseDto coinTypeEditList(Integer coinTypeId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(coinCommonService.createCoinCompletedInfo(coinTypeId));
        return baseDto;
    }

    @RequestMapping("/cointype/edit")
    public void coinTypeEdit(HttpServletResponse response,
                             Integer coinTypeId,
                             BigDecimal singlePrice,
                             BigDecimal todayDealCount,
                             BigDecimal totalDealCount) throws IOException {
        //Jedis //jedis = jedisUtil.getJedis();
        String now = LocalDateTime.now().toString();
        jedisUtil.set(RedisKeyCreator.openPrice(coinTypeId), singlePrice.toString());
        jedisUtil.set(RedisKeyCreator.openPriceUpdateTime(coinTypeId), now);
        jedisUtil.set(RedisKeyCreator.todayDealCount(coinTypeId), todayDealCount.toString());
        jedisUtil.set(RedisKeyCreator.todayDealCountUpdateTime(coinTypeId), now);
        jedisUtil.set(RedisKeyCreator.totalDealCount(coinTypeId), totalDealCount.toString());
        jedisUtil.set(RedisKeyCreator.totalDealCountUpdateTime(coinTypeId), now);
        //jedisUtil.close();

        response.sendRedirect("listPage");

    }


    @RequestMapping("/cointype/del")
    public BaseDto coinTypeDel(HttpServletResponse response ,
                            Integer coinTypeId) throws IOException {
        BaseDto baseDto = new BaseDto();
        List<UserCapital> userCapitals = userCapitalService.getsByCoinType(coinTypeId);
        if (userCapitals==null || userCapitals.isEmpty()){
            coinTypeService.delete(coinTypeId);
            return BaseDto.ok("删除成功");
        }else {
            return BaseDto.error("用户正持有该货币");
        }
    }



    @RequestMapping("/cointype/icon/upload")
    public BaseDto uploadCoinIcon(@RequestParam("iconFile") MultipartFile icon) throws Exception {
        String fileName = icon.getOriginalFilename();
        String suffix = FileUtil.getFileExtName(fileName);
        InputStream inputStream = icon.getInputStream();
        byte[] iconContent = new byte[(int) icon.getSize()];
        inputStream.read(iconContent);
        inputStream.close();
        String bucketName = OssConstant.ossBucketName;
        String objectName = "coin-" + UUID.randomUUID().toString().substring(0, 10);
        ossCommonService.uploadFile(bucketName, objectName, iconContent, suffix);
        String url = ossCommonService.getUrl(bucketName, objectName);
        Map<String, String> map = new HashMap<>();
        map.put("objectName", objectName);
        map.put("url", url);
        return BaseDto.ok(map);
    }

    /**
     * 新增币种
     *
     * @param response
     * @param name
     * @param icon
     * @param price
     * @throws IOException
     */
    @RequestMapping("/cointype/add")
    public void cointypeAdd(HttpServletResponse response,
                            String name,
                            String icon,
                            String simpleName,
                            BigDecimal price
    ) throws IOException {
        CoinType coinType = new CoinType();
        coinType.setName(name);
        coinType.setSimpleName(simpleName);
        coinType.setIcon(icon);
        coinTypeService.insert(coinType);
        OpenPriceHistory openPriceHistory = new OpenPriceHistory();
        openPriceHistory.setOpenPrice(price);
        openPriceHistory.setCoinTypeId(coinType.getId());

        openPriceHistoryService.insert(openPriceHistory);
        int coinTypeId = coinType.getId();
        //Jedis //jedis = jedisUtil.getJedis();
        String now = LocalDateTime.now().toString();
        jedisUtil.set(RedisKeyCreator.openPrice(coinTypeId), price.toString());
        jedisUtil.set(RedisKeyCreator.openPriceUpdateTime(coinTypeId), now);

        /** 插入手续费 **/
        feeService.insert(coinTypeId,DealScene.HANG_BUY,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.HANG_SALE,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.SHAREHOLDER_TRANSFER_SHAREHOLDER,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.MEMBER_TRANSFER_MEMBER,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.MEMBER_TRANSFER_SALLER,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.SALLER_TRANSFER_SALLER,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.SALLER_TRANSFER_MEMBER,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.SYSTEM_BUY_BACK,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.USER_BUY,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.SYSTEM_BUY_BACK_COMMISSION,new BigDecimal(0),new BigDecimal(0));
        feeService.insert(coinTypeId,DealScene.FROZEN_BUY_BACK,new BigDecimal(0),new BigDecimal(0));

        response.sendRedirect("listpage");
    }

    /**
     * 设置回购服务费率
     *
     * @param response
     * @param buyBackServiceFeeRatio
     * @throws IOException
     */
    @RequestMapping("/raito/buyBackServiceFee")
    public void setBuyBackServiceFeeRatio(HttpServletResponse response,
                                          BigDecimal buyBackServiceFeeRatio) throws IOException {
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.set(RedisKeyCreator.buyBackServiceFeeRatio(), buyBackServiceFeeRatio.toString());
        //jedisUtil.close();
        response.sendRedirect("buyBackServiceFeePage");
    }

    /**
     * 回购服务费率列表
     *
     * @return
     */
    @RequestMapping("/raito/buyBackServiceFeeList")
    public BaseDto getBuyBackServiceFeeRatio() {
        BaseDto baseDto = new BaseDto();
        //Jedis //jedis = jedisUtil.getJedis();
        String buyBackServiceFeeRatio = jedisUtil.get(RedisKeyCreator.buyBackServiceFeeRatio());
        //jedisUtil.close();
        baseDto.setData(buyBackServiceFeeRatio);
        return baseDto;
    }

    /**
     * 设置会员钱包容量
     *
     * @param response
     * @param maxCount
     * @param minCount
     * @throws IOException
     */
    @RequestMapping("/wallet/member")
    public void setwalletkeepcount(HttpServletResponse response,
                                   BigDecimal maxCount,
                                   BigDecimal minCount) throws IOException {
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.set(RedisKeyCreator.memberKeepAliveCoinMaxCount(), maxCount.toString());
        jedisUtil.set(RedisKeyCreator.memberKeepAliveCoinMinCount(), minCount.toString());
        //jedisUtil.close();
        response.sendRedirect("memberPage");
    }

    /**
     * 设置会员钱包容量页面
     *
     * @return
     */
    @RequestMapping("/wallet/memberList")
    public BaseDto getMemberList() {
        BaseDto baseDto = new BaseDto();
        //Jedis //jedis = jedisUtil.getJedis();
        String maxCount = jedisUtil.get(RedisKeyCreator.memberKeepAliveCoinMaxCount());
        String minCount = jedisUtil.get(RedisKeyCreator.memberKeepAliveCoinMinCount());
        //jedisUtil.close();
        Map map = new HashMap();
        map.put("maxCount", maxCount);
        map.put("minCount", minCount);
        baseDto.setData(map);
        return baseDto;
    }

    /**
     * 商家钱包限额
     *
     * @param response
     * @param sellerWalletLimitCount
     * @throws IOException
     */
    @RequestMapping("/wallet/seller")
    public void setWalletSellerKeepcount(HttpServletResponse response,
                                         BigDecimal sellerWalletLimitCount) throws IOException {
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.set(RedisKeyCreator.sellerWalletLimitCount(), sellerWalletLimitCount.toString());
        //jedisUtil.close();
        response.sendRedirect("sellerPage");
    }

    /**
     * 商家钱包限额页面
     */
    @RequestMapping("/wallet/sellerList")
    public BaseDto setWalletSellerKeepcountList() {
        BaseDto baseDto = new BaseDto();
        //Jedis //jedis = jedisUtil.getJedis();
        String sellerWalletLimitCount = jedisUtil.get(RedisKeyCreator.sellerWalletLimitCount());
        //jedisUtil.close();
        baseDto.setData(sellerWalletLimitCount);
        return baseDto;
    }

    /**
     * 新钱包激活
     * @param activateWalletNeedMinCount
     * @param activateWalletNeedMaxCount
     * @param activeTime
     * @param shortCount
     * @param shortDeadLine
     */
    @RequestMapping("/wallet/active")
    public void activeMinCount(HttpServletResponse response,
                               BigDecimal activateWalletNeedMinCount,
                               BigDecimal activateWalletNeedMaxCount,
                               Integer activeTime,
                               Integer shortCount,
                               Integer shortDeadLine) throws IOException {
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.set(RedisKeyCreator.activateWalletNeedMinCount(), activateWalletNeedMinCount.toString());
        jedisUtil.set(RedisKeyCreator.activateWalletNeedMaxCount(), activateWalletNeedMaxCount.toString());
        jedisUtil.set(RedisKeyCreator.activateTime(), String.valueOf(activeTime));
        jedisUtil.set(RedisKeyCreator.shortCount(), String.valueOf(shortCount));
        jedisUtil.set(RedisKeyCreator.shortDeadline(), String.valueOf(shortDeadLine));
        //jedisUtil.close();
        response.sendRedirect("activePage");
    }

    /**
     * 新钱包激活页面
     */
    @RequestMapping("/wallet/activeList")
    public BaseDto activeMinCount() {
        //Jedis //jedis = jedisUtil.getJedis();
        String activateWalletNeedMinCount = jedisUtil.get(RedisKeyCreator.activateWalletNeedMinCount());
        String activateWalletNeedMaxCount = jedisUtil.get(RedisKeyCreator.activateWalletNeedMaxCount());
        String activeTime = jedisUtil.get(RedisKeyCreator.activateTime());
        String shortCount = jedisUtil.get(RedisKeyCreator.shortCount());
        String shortDeadLine = jedisUtil.get(RedisKeyCreator.shortDeadline());
        //jedisUtil.close();
        BaseDto baseDto = new BaseDto();
        Map map = new HashMap();
        map.put("activateWalletNeedMinCount", activateWalletNeedMinCount);
        map.put("activateWalletNeedMaxCount", activateWalletNeedMaxCount);
        map.put("activeTime", activeTime);
        map.put("shortCount", shortCount);
        map.put("shortDeadLine", shortDeadLine);
        baseDto.setData(map);
        return baseDto;
    }

    // TODO: 2019/4/19 这里页面需要一个币种id

//    /**
//     * 会员冻结考核周期
//     * 会员冻结最低动销率
//     *
//     * @throws IOException
//     */
//    @RequestMapping("/frozen/m")
//    public BaseDto  m(Integer m_examinationCycle,
//                  BigDecimal m_movingPin,
//                  @RequestParam(value = "coinTypeId", defaultValue = "1") Integer coinTypeId)  {
//        BaseDto baseDto = new BaseDto();
//        jedisUtil.set(RedisKeyCreator.memberExaminationCycle(coinTypeId), m_examinationCycle.toString());
//        jedisUtil.set(RedisKeyCreator.memberMovingPin(coinTypeId), m_movingPin.toString());
//        baseDto.setData("设置成功");
//        baseDto.setCode(0);
//        return baseDto;
//    }
//

    @RequestMapping("/frozen/m")
    public BaseDto frozenM(HttpServletResponse response,
                                          BigDecimal m_examinationCycle,
                                          BigDecimal m_movingPin,
                                          BigDecimal m_frozenBuyBackFee) throws IOException {
        BaseDto baseDto = new BaseDto();
            jedisUtil.set(RedisKeyCreator.m_examinationCycle(),m_examinationCycle.toString());
            jedisUtil.set(RedisKeyCreator.m_movingPin(),m_movingPin.toString());
            jedisUtil.set(RedisKeyCreator.m_frozenBuyBackFee(),m_frozenBuyBackFee.toString());
            baseDto.setData("设置成功");
        response.sendRedirect("/back/dealparam/other/frozenPage");

        return baseDto;

    }

    @RequestMapping("/frozen/mList")
    public BaseDto frozenMList() {
        //Jedis //jedis = jedisUtil.getJedis();
        String m_examinationCycle = jedisUtil.get(RedisKeyCreator.m_examinationCycle());
        String m_movingPin = jedisUtil.get(RedisKeyCreator.m_movingPin());
        String m_frozenBuyBackFee = jedisUtil.get(RedisKeyCreator.m_frozenBuyBackFee());
        Map map = new HashMap();
        map.put("m_examinationCycle", m_examinationCycle);
        map.put("m_movingPin", m_movingPin);
        map.put("m_frozenBuyBackFee", m_frozenBuyBackFee);
        BaseDto baseDto = new BaseDto();
        baseDto.setData(map);
        return baseDto;
    }
    // TODO: 2019/4/19 页面需要一个coinTypeId

    /**
     * 股东冻结考核周期
     * 股东冻结最低动销率
     */
    @RequestMapping("/frozen/s")
    public BaseDto frozenS(HttpServletResponse response,
                           BigDecimal s_examinationCycle,
                           BigDecimal s_movingPin,
                           BigDecimal s_frozenBuyBackFee) throws IOException {
        BaseDto baseDto = new BaseDto();
        jedisUtil.set(RedisKeyCreator.s_examinationCycle(),s_examinationCycle.toString());
        jedisUtil.set(RedisKeyCreator.s_movingPin(),s_movingPin.toString());
        jedisUtil.set(RedisKeyCreator.s_frozenBuyBackFee(),s_frozenBuyBackFee.toString());
        baseDto.setData("设置成功");
        response.sendRedirect("/back/dealparam/other/s_frozenPage");
        return baseDto;
    }

    @RequestMapping("/frozen/sList")
    public BaseDto frozenSList() {
        //Jedis //jedis = jedisUtil.getJedis();
        String s_examinationCycle = jedisUtil.get(RedisKeyCreator.s_examinationCycle());
        String s_movingPin = jedisUtil.get(RedisKeyCreator.s_movingPin());
        String s_frozenBuyBackFee = jedisUtil.get(RedisKeyCreator.s_frozenBuyBackFee());
        Map map = new HashMap();
        map.put("s_examinationCycle", s_examinationCycle);
        map.put("s_movingPin", s_movingPin);
        map.put("s_frozenBuyBackFee", s_frozenBuyBackFee);
        BaseDto baseDto = new BaseDto();
        baseDto.setData(map);
        return baseDto;
    }


    /**
     * 手续费列表
     *
     * @return
     */
    @RequestMapping("/fee/list")
    public BaseDto getFeeList() {
        BaseDto baseDto = new BaseDto();
        List<Fee> all = feeService.getAll();
        baseDto.setData(all);
        return baseDto;
    }


    @RequestMapping("/fee/coinTypeId")
    public BaseDto getByCoinTypeId(Integer coinTypeId) {
        BaseDto baseDto = new BaseDto();
        List<Fee> all = feeService.getByCoinTypeId(coinTypeId);
        baseDto.setData(all);
        return baseDto;
    }

    /**
     * 添加手续费
     *
     * @param coinTypeId
     * @param dealScene
     * @param baseDealCount
     * @param feeRaito
     * @param response
     * @throws IOException
     */
    @RequestMapping("/fee/add")
    public void addFee(Integer coinTypeId,
                       Integer dealScene,
                       BigDecimal baseDealCount,
                       BigDecimal feeRaito,
                       HttpServletResponse response) throws IOException {
        Fee fee = new Fee();
        fee.setCoinTypeId(coinTypeId);
        fee.setDealScene(dealScene);
        fee.setBaseDealCount(baseDealCount);
        fee.setFeeRatio(feeRaito);
        feeService.insert(fee);
        response.sendRedirect("listPage");
    }

    /**
     * 单个手续费详情
     *
     * @param feeId
     * @param response
     * @return
     */
    @RequestMapping("/fee/detail")
    public BaseDto editFee(Integer feeId,
                           HttpServletResponse response) throws IOException {
        Fee fee = feeService.getByPrimaryKey(feeId);
        BaseDto baseDto = new BaseDto();
        baseDto.setData(fee);
        response.sendRedirect("listPage");
        return baseDto;
    }

    /**
     * 批量设置手续费
     */
    @RequestMapping("/fee/setAll")
    public BaseDto feeSetAll(@RequestParam("list") String text){
        List<RaitoList> list = JSON.parseArray(text,RaitoList.class);
        feeService.setAll(list);
        BaseDto baseDto = new BaseDto();
        baseDto.setCode(CodeConst.OK);
        return baseDto;
    }

//    @RequestMapping("/fee/setUserList")
//    public void setUser(Integer coinTypeId,
//                        Integer dealScene,
//                        List<Integer> userIdList,
//                        BigDecimal feeRaito,
//                        HttpServletResponse response) throws IOException {
//        for (Integer userId : userIdList) {
//            Fee fee = feeService.getFee(userId, dealScene, coinTypeId);
//            fee.setFeeRatio(feeRaito);
//            fee.setUpdateTime(LocalDateTime.now());
//            feeService.update(fee);
//        }
//        response.sendRedirect("listPage");
//    }

    //redis key:    memberKeepAliveCoinMinCount

    /**
     * 活币最小,最大持有量设置
     *
     * @param memberKeepAliveCoinMinCount 活币最小持有量
     * @return
     */
    @RequestMapping("/wallet/aliveCountset")
    public BaseDto walletAliveMinCountset(HttpServletResponse response,
                                          BigDecimal memberKeepAliveCoinMinCount,
                                          BigDecimal memberKeepAliveCoinMaxCount) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            //Jedis //jedis = jedisUtil.getJedis();
            jedisUtil.set(RedisKeyCreator.memberKeepAliveCoinMinCount(), memberKeepAliveCoinMinCount.toString());
            jedisUtil.set(RedisKeyCreator.memberKeepAliveCoinMaxCount(), memberKeepAliveCoinMaxCount.toString());
            //jedisUtil.close();
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("aliveCountPage");
        return baseDto;
    }

    @RequestMapping("/wallet/aliveCountList")
    public BaseDto walletAliveCountList() {
        //Jedis //jedis = jedisUtil.getJedis();
        BaseDto baseDto = new BaseDto();
        String memberKeepAliveCoinMinCount = jedisUtil.get(RedisKeyCreator.memberKeepAliveCoinMinCount());
        String memberKeepAliveCoinMaxCount = jedisUtil.get(RedisKeyCreator.memberKeepAliveCoinMaxCount());
        //jedisUtil.close();
        Map map = new HashMap();
        map.put("memberKeepAliveCoinMinCount", memberKeepAliveCoinMinCount);
        map.put("memberKeepAliveCoinMaxCount", memberKeepAliveCoinMaxCount);
        baseDto.setData(map);
        return baseDto;
    }
    //redis key:   hangSaleBuyBackRate

    /**
     * 挂卖回购系数设置
     *
     * @param hangSaleBuyBackRate 挂卖回购系数
     * @return
     */
    @RequestMapping("/raito/hangSaleBuyBack")
    public BaseDto hangSaleBuyBack(HttpServletResponse response, BigDecimal hangSaleBuyBackRate) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            //Jedis //jedis = jedisUtil.getJedis();
            jedisUtil.set(RedisKeyCreator.hangSaleBuyBackRate(), hangSaleBuyBackRate.toString());
            //jedisUtil.close();
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("hangSaleBuyBackPage");
        return baseDto;
    }

    @RequestMapping("/raito/hangSaleBuyBackList")
    public BaseDto hangSaleBuyBackList() {
        //Jedis //jedis = jedisUtil.getJedis();
        String hangSaleBuyBackRate = jedisUtil.get(RedisKeyCreator.hangSaleBuyBackRate());
        //jedisUtil.close();
        BaseDto baseDto = new BaseDto();
        baseDto.setData(hangSaleBuyBackRate);
        return baseDto;
    }

    /**
     * 挂买出价系数
     *
     * @param response
     * @param outPrice
     * @throws IOException
     */
    @RequestMapping("/raito/hangbuyoutprice")
    public void setHangBuyOutPrice(HttpServletResponse response,
                                   BigDecimal outPrice) throws IOException {
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.set(RedisKeyCreator.hangBuyOutPriceRate(), outPrice.toString());
        //jedisUtil.close();
        response.sendRedirect("/back/dealparam/raito/hangBuyOutPricePage");
    }

    /**
     * 挂买出价系数
     *
     * @throws IOException
     */
    @RequestMapping("/raito/hangbuyoutpricelist")
    public BaseDto hangbuyoutpricelist() {
        //Jedis //jedis = jedisUtil.getJedis();
        String hangSaleBuyBackRate = jedisUtil.get(RedisKeyCreator.hangBuyOutPriceRatio());
        //jedisUtil.close();
        BaseDto baseDto = new BaseDto();
        baseDto.setData(hangSaleBuyBackRate);
        return baseDto;
    }
    /**
     * 挂卖出价系数
     *
     * @param response
     * @param limitPriceSale
     */
    @RequestMapping("/raito/limitPriceSale")
    public void limitPriceSale(HttpServletResponse response,
                                   BigDecimal limitPriceSale) throws IOException {
        jedisUtil.set(RedisKeyCreator.limitPriceSale(), limitPriceSale.toString());
        BaseDto baseDto = new BaseDto();
        baseDto.setCode(0);
        response.sendRedirect("/back/dealparam/raito/limitPriceSalePage");
    }

    /**
     * 挂买出价系数
     */
    @RequestMapping("/raito/limitPriceSaleList")
    public BaseDto limitPriceSaleList() {
        String limitPriceSale = jedisUtil.get(RedisKeyCreator.limitPriceSale());
        BaseDto baseDto = new BaseDto();
        baseDto.setData(limitPriceSale);
        return baseDto;
    }

    /**
     * 配发周期设置
     *
     * @param response
     * @param statrTime
     * @param dayCount
     * @throws IOException
     */
    @RequestMapping("/distribute/cycle")
    public void setDistributeCycle(HttpServletResponse response,
                                   LocalDateTime statrTime,
                                   Integer dayCount) throws IOException {
        jedisUtil.set(RedisKeyCreator.grantIntervalTime(), String.valueOf(dayCount));
        jedisUtil.set(RedisKeyCreator.startGrantTime(), statrTime.toString());
        System.out.println(statrTime);
        System.out.println(statrTime.toString());
        response.sendRedirect("distributepage");
    }


    /**
     * 获取历史价格记录
     *
     * @return
     */
    @RequestMapping("/history/price")
    public BaseDto historyPrice() {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(openPriceHistoryService.getAll());
        return baseDto;
    }

    //    redis key:   hangBuyOutPriceRate

    /**
     * 挂买出价系数设置
     *
     * @param hangBuyOutPriceRate 挂买出价系数
     * @return
     */
    @RequestMapping("/raito/hangBuyOutPrice")
    public BaseDto hangBuyOutPriceRate(HttpServletResponse response, BigDecimal hangBuyOutPriceRate) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            jedisUtil.set(RedisKeyCreator.hangBuyOutPriceRate(), hangBuyOutPriceRate.toString());
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("hangBuyOutPricePage");
        return baseDto;
    }


    @RequestMapping("/raito/hangBuyOutPriceList")
    public BaseDto hangBuyOutPriceList() {
        String hangBuyOutPriceRate = jedisUtil.get(RedisKeyCreator.hangBuyOutPriceRate());
        BaseDto baseDto = new BaseDto();
        baseDto.setData(hangBuyOutPriceRate);
        return baseDto;
    }


    /**
     * 挂买违约金系数设置
     * 违约金扣除时间设置
     * @param penaltyForBreachRate 违约金系数
     * @param penaltyForBreachTime 支付超时时间
     * @return
     */
    @RequestMapping("/raito/penaltyForBreach")
    public BaseDto penaltyForBreachRate(HttpServletResponse response,
                                        BigDecimal penaltyForBreachRate,
                                        BigDecimal penaltyForBreachTime) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            //Jedis //jedis = jedisUtil.getJedis();
            jedisUtil.set(RedisKeyCreator.penaltyForBreachRate(), penaltyForBreachRate.toString());
            jedisUtil.set(RedisKeyCreator.penaltyForBreachTime(), penaltyForBreachTime.toString());
            //jedisUtil.close();
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("penaltyForBreachPage");
        return baseDto;
    }

    @RequestMapping("/raito/penaltyForBreachList")
    public BaseDto penaltyForBreachList() {
        //Jedis //jedis = jedisUtil.getJedis();
        String penaltyForBreachRate = jedisUtil.get(RedisKeyCreator.penaltyForBreachRate());
        String penaltyForBreachTime = jedisUtil.get(RedisKeyCreator.penaltyForBreachTime());
        //jedisUtil.close();
        Map map = new HashMap();
        map.put("penaltyForBreachRate", penaltyForBreachRate);
        map.put("penaltyForBreachTime", penaltyForBreachTime);
        BaseDto baseDto = new BaseDto();
        baseDto.setData(map);
        return baseDto;
    }

    /**
     * 商家保证金
     *
     * @param sellerDeposit
     * @param sellerDepositCondition
     * @return
     * @throws IOException
     */
    @RequestMapping("/raito/sellerDeposit")
    public void sellerDeposit(HttpServletResponse response,
                              BigDecimal sellerDeposit,
                              BigDecimal sellerDepositCondition) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            //Jedis //jedis = jedisUtil.getJedis();
            jedisUtil.set(RedisKeyCreator.sellerDeposit(), sellerDeposit.toString());
            jedisUtil.set(RedisKeyCreator.sellerDepositCondition(), sellerDepositCondition.toString());
            //jedisUtil.close();
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("sellerDepositPage");
    }

    @RequestMapping("/raito/sellerDepositList")
    public BaseDto sellerDepositList() {
        //Jedis //jedis = jedisUtil.getJedis();
        String sellerDeposit = jedisUtil.get(RedisKeyCreator.sellerDeposit());
        String sellerDepositCondition = jedisUtil.get(RedisKeyCreator.sellerDepositCondition());
        //jedisUtil.close();
        Map map = new HashMap();
        map.put("sellerDeposit", sellerDeposit);
        map.put("sellerDepositCondition", sellerDepositCondition);
        BaseDto baseDto = new BaseDto();
        baseDto.setData(map);
        return baseDto;
    }

    /**
     * 配发周期
     *
     * @param grantIntervalTime
     * @return
     */
    @RequestMapping("/other/grantIntervalTime")
    public BaseDto grantIntervalTime(HttpServletResponse response, Integer coinTypeId, Integer grantIntervalTime,String startGrantTime) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            //Jedis //jedis = jedisUtil.getJedis();
            jedisUtil.set(RedisKeyCreator.grantIntervalTime(coinTypeId), String.valueOf(grantIntervalTime));
            if (StringUtils.hasText(startGrantTime))
            {
                LocalDateTime localDateTime = LocalDateTime.parse(startGrantTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                jedisUtil.set(RedisKeyCreator.startGrantTime(coinTypeId), localDateTime.toString());
            }
            //jedisUtil.close();
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("grantIntervalTimePage");
        return baseDto;
    }

    @RequestMapping("/other/grantIntervalTimeList")
    public BaseDto grantIntervalTimeList(@RequestParam(defaultValue = "1",value = "coinTypeId")Integer coinTypeId) {
        //Jedis //jedis = jedisUtil.getJedis();
        String grantIntervalTime = jedisUtil.get(RedisKeyCreator.grantIntervalTime(coinTypeId));
        String startGrantTime = jedisUtil.get(RedisKeyCreator.startGrantTime(coinTypeId));
        Map map = new HashMap();
        map.put("grantIntervalTime", grantIntervalTime);
        map.put("startGrantTime", startGrantTime);
        BaseDto baseDto = new BaseDto();
        baseDto.setData(map);
        return baseDto;
    }

    //rechargeBaseCount";withdrawBaseCount";

    /**
     * 最低充值金额
     * @param rechargeBaseCount
     */
    @RequestMapping("/other/rechargeBaseCount")
    public BaseDto rechargeBaseCount(HttpServletResponse response,
                                     BigDecimal rechargeBaseCount,
                                     Integer rechargeOverTime) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            //Jedis //jedis = jedisUtil.getJedis();
            jedisUtil.set(RedisKeyCreator.rechargeBaseCount(), rechargeBaseCount.toString());
            jedisUtil.set(RedisKeyCreator.rechargeOverTime(), rechargeOverTime.toString());
            //jedisUtil.close();
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("rechargeBaseCountPage");
        return baseDto;
    }

    @RequestMapping("/other/rechargeBaseCountList")
    public BaseDto rechargeBaseCountList() {
        //Jedis //jedis = jedisUtil.getJedis();
        String rechargeBaseCount = jedisUtil.get(RedisKeyCreator.rechargeBaseCount());
        String rechargeOverTime = jedisUtil.get(RedisKeyCreator.rechargeOverTime());
        BaseDto baseDto = new BaseDto();
        Map map = new HashMap();
        map.put("rechargeBaseCount",rechargeBaseCount);
        map.put("rechargeOverTime",rechargeOverTime);
        baseDto.setData(map);
        return baseDto;
    }
    /**
     * 最低提现金额
     * @param withdrawBaseCount
     */
    @RequestMapping("/other/withdrawBaseCount")
    public BaseDto withdrawBaseCountCount(HttpServletResponse response,
                                          BigDecimal withdrawBaseCount,
                                          BigDecimal userWithdrawFeeRatio) throws IOException {
        BaseDto baseDto = new BaseDto();
        try {
            //Jedis //jedis = jedisUtil.getJedis();
            jedisUtil.set(RedisKeyCreator.withdrawBaseCount(), withdrawBaseCount.toString());
            jedisUtil.set(RedisKeyCreator.userWithdrawFeeRatio(),userWithdrawFeeRatio.toString());
            //jedisUtil.close();
            baseDto.setData("设置成功");
        } catch (Exception e) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("设置失败");
        }
        response.sendRedirect("withdrawBaseCountPage");
        return baseDto;
    }

    @RequestMapping("/other/withdrawBaseCountList")
    public BaseDto withdrawBaseCountList() {
        //Jedis //jedis = jedisUtil.getJedis();
        String withdrawBaseCount = jedisUtil.get(RedisKeyCreator.withdrawBaseCount());
        String userWithdrawFeeRatio = jedisUtil.get(RedisKeyCreator.userWithdrawFeeRatio());
        Map map = new HashMap();
        map.put("withdrawBaseCount", withdrawBaseCount);
        map.put("userWithdrawFeeRatio", userWithdrawFeeRatio);
        BaseDto baseDto = new BaseDto();
        baseDto.setData(map);
        return baseDto;
    }

    /**
     * 销户
     * @param response
     * @param userDeleteReturnMoneyFee
     * @throws IOException
     */
    @RequestMapping("/raito/userDeleteReturnMoneyFee")
    public void userDeleteReturnMoneyFee(HttpServletResponse response,BigDecimal userDeleteReturnMoneyFee) throws IOException {
        BaseDto baseDto = new BaseDto();
        jedisUtil.set(RedisKeyCreator.userDeleteReturnMoneyFee(),userDeleteReturnMoneyFee.toString());
        baseDto.setData("设置成功");
        response.sendRedirect("userDeleteReturnMoneyFeePage");
    }

    @RequestMapping("/raito/userDeleteReturnMoneyFeeList")
    public BaseDto userDeleteReturnMoneyFeeList() {
        String coinRatioD = jedisUtil.get(RedisKeyCreator.userDeleteReturnMoneyFee());
        BaseDto baseDto = new BaseDto();
        baseDto.setData(coinRatioD);
        return baseDto;
    }

    /**
     * 单笔最低出账
     * @param response
     * @param memberOnceMinOutCount
     * @throws IOException
     */
    @RequestMapping("/raito/memberOnceMinOutCount")
    public void memberOnceMinOutCount(HttpServletResponse response,BigDecimal memberOnceMinOutCount) throws IOException {
        BaseDto baseDto = new BaseDto();
        jedisUtil.set(RedisKeyCreator.memberOnceMinOutCount(),memberOnceMinOutCount.toString());
        baseDto.setData("设置成功");
        response.sendRedirect("memberOnceMinOutCountPage");
    }

    @RequestMapping("/raito/memberOnceMinOutCountList")
    public BaseDto memberOnceMinOutCountList() {
        String memberOnceMinOutCount = jedisUtil.get(RedisKeyCreator.memberOnceMinOutCount());
        BaseDto baseDto = new BaseDto();
        baseDto.setData(memberOnceMinOutCount);
        return baseDto;
    }

    @RequestMapping("/raito/priceIncrease")
    public BaseDto raitoPriceIncrease(String priceIncrease){
        List<CoinRatio> coinRaitoList = JSON.parseArray(priceIncrease,CoinRatio.class);
        for (CoinRatio coinRaitos : coinRaitoList) {
            Integer id = coinRaitos.getId();
            CoinRatio coinRatio = new CoinRatio();
            if (id!=null){
                coinRatio = coinRatioService.getByPrimaryKey(id);
                coinRatio.setLow(coinRaitos.getLow());
                coinRatio.setHigh(coinRaitos.getHigh());
                coinRatio.setValue(coinRaitos.getValue());
                coinRatioService.update(coinRatio);
            }else {
                coinRatio.setCoinTypeId(coinRaitos.getCoinTypeId());
                coinRatio.setLow(coinRaitos.getLow());
                coinRatio.setHigh(coinRaitos.getHigh());
                coinRatio.setValue(coinRaitos.getValue());
                coinRatio.setName(coinRaitos.getName());
                coinRatioService.insert(coinRatio);
            }
        }
        BaseDto baseDto = new BaseDto();
        baseDto.setCode(0);
        return baseDto;
    }

    @RequestMapping("/raito/priceIncreaseList")
    public BaseDto raitoPriceIncreaseList(@RequestParam(defaultValue = "1",value = "coinTypeId") int coinTypeId){
        BaseDto baseDto = new BaseDto();
        List<CoinRatio> all = coinRatioService.getAll();
        Map map = new HashMap();
        String coinRatito = jedisUtil.get(RedisKeyCreator.CoinRatioD(coinTypeId));
        map.put("ABC",all);
        map.put("D",coinRatito);
        return baseDto;
    }

    @RequestMapping("/raito/coinRaitoD")
    public void CoinRatioD(HttpServletResponse response,
                           BigDecimal coinRatioD,
                           @RequestParam(defaultValue = "1",value = "coinTypeId") int coinTypeId ) throws IOException {
        BaseDto baseDto = new BaseDto();
        jedisUtil.set(RedisKeyCreator.CoinRatioD(coinTypeId),coinRatioD.toString());
        baseDto.setData("设置成功");
        response.sendRedirect("/back/dealparam/raito/coinRatioDPage");
    }

    @RequestMapping("/raito/coinRaitoDList")
    public BaseDto CoinRatioDList(@RequestParam(defaultValue = "1",value = "coinTypeId") int coinTypeId) {
        String coinRatioD = jedisUtil.get(RedisKeyCreator.CoinRatioD(coinTypeId));
        BaseDto baseDto = new BaseDto();
        baseDto.setData(coinRatioD);
        return baseDto;
    }


}
